#include "gui.h"

gui_manager* gui_init_gui() {
    gui_manager *gm;
    gm = g_malloc0(sizeof (gui_manager));
    gm->buttons = NULL;
    return gm;
}

void gui_manager_update(main_obj *mobj) {
    int i = 0;
    int l = g_list_length(mobj->gm->elements);
    if (mobj->event->type == SDL_MOUSEBUTTONDOWN) {
        //g_debug("mouse_cliked");
        for (i = 0; i < l; i++) {
            graphic_element *ge = (graphic_element*) g_list_nth_data(mobj->gm->elements, i);
            if (gui_util_element_is_clicked(mobj, ge)) {
                ge->focused = TRUE;
            } else {
                ge->focused = FALSE;
            }
        }
    }
    for (i = 0; i < l; i++) {
        graphic_element *ge = (graphic_element*) g_list_nth_data(mobj->gm->elements, i);
        switch (ge->type) {
            case (GUI_BUTTON):
            {
                button *b = (button*) ge;
                b->update(mobj, b);
                break;
            }
            case (GUI_TEXTBOX):
            {
                textbox *t = (textbox*) ge;
                t->update(mobj, t);
                break;
            }
            case (GUI_LABEL):
            {
                label *l = (label*) ge;
                l->update(mobj, l);
                break;
            }
            case (GUI_NOTEBOOK):
            {
                notebook *n = (notebook*) ge;
                n->update(mobj, n);
                break;
            }
        }
    }
}

void gui_manager_update_graphics(main_obj * mobj) {
    int i = 0;
    SDL_Rect rect;
    int l = g_list_length(mobj->gm->elements);
    for (i = 0; i < l; i++) {
        gpointer p = g_list_nth_data(mobj->gm->elements, i);
        graphic_element *ge = (graphic_element*) p;
        /* verifica che l'elemento sia da ridisegnare */
        if (ge->redraw) {
            switch (ge->type) {
                case (GUI_TEXTBOX):
                {
                    //g_debug("updating graphi textbox %d", i);
                    textbox *t = (textbox*) p;
                    t->paint(mobj, t);
                    //g_debug("drawed %d textbox", i);
                    break;
                }
                case (GUI_LABEL):
                {
                    label *l = (label*) p;
                    l->paint(mobj, l);
                    break;
                }
                case (GUI_BUTTON):
                {
                    button *b = (button*) p;
                    b->paint(mobj, b);
                    break;
                }
                case (GUI_NOTEBOOK):
                {
                    notebook *n = (notebook*) p;
                    n->paint(mobj, n);
                    break;
                }
            }
        }
        ge->redraw = FALSE;
        rect.x = ge->posx;
        rect.y = ge->posy;
        SDL_BlitSurface(ge->graphic, 0, mobj->main_screen, &rect);
        p = NULL;
    }
}

void gui_add_element(main_obj *mobj, gpointer el) {
    graphic_element *ge = (graphic_element*) el;
    switch (ge->type) {
        case GUI_BUTTON:
        {
            gui_add_button(mobj, (button*) el);
            break;
        }
        case GUI_TEXTBOX:
        {
            //g_debug("added textbox to list lenght: %d address: %x", g_list_length(mobj->gm->elements), el);
            mobj->gm->elements = g_list_append(mobj->gm->elements, el);
            break;
        }
        default:
        {
            mobj->gm->elements = g_list_append(mobj->gm->elements, el);
            break;
        }
    }
}

void gui_add_button(main_obj *mobj, button *b) {
    /* TODO elminare supporto a liste multiple */
    //mobj->gm->buttons = g_list_append(mobj->gm->buttons, b);
    mobj->gm->elements = g_list_append(mobj->gm->elements, b);
}

void gui_update_login_textbox() {

}
